/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import java.util.Calendar;
import java.util.Date;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

//incomplete , must implement traverse
public class DateField extends TraversableWidget {
	private Calendar calendar;
	private final byte FIELD_DAY = 10;
	private byte currentField = FIELD_DAY;
	private String date;
	private int day;

	private final byte FIELD_MONTH = 20;
	private final byte FIELD_YEAR = 30;
	private Font highlitFont;

	private boolean isEditing = false;

	private int month;

	private int selectedBgColor = 0;
	private int selectedForeColor = 0xffffff;
	private int selectorHeight;
	private int selectorWidth;
	private int selectorX;
	private int year;

	public DateField(Theme theme) {
		super(theme, 0, 0);
		updateModel();
	}

	public void cleanUp() {
	// widgetFont = null;
	}

	public Date getDate() {
		return null;

	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		g.setColor(selectedBgColor);
		g.fillRect(selectorX, 2, selectorWidth, selectorHeight);
		int currX = 2;
		g.setFont((isEditing && currentField == FIELD_DAY) ? highlitFont : theme.widgetFont);
		g.setColor((isEditing && currentField == FIELD_DAY) ? selectedForeColor : theme.fgColor);
		g.drawString(Integer.toString(day), currX, 2, 0);
		currX += g.getFont().stringWidth(Integer.toString(day));

		// switch (currentField) {
		// case FIELD_DAY:
		// // g.setFont(Font.getFont(widgetFont.getFace(), Font.STYLE_BOLD,
		// // widgetFont.getSize()));
		// g.drawString(Integer.toString(day), currX, 2, 0);
		//				
		// break;
		// case FIELD_MONTH:
		// break;
		// case FIELD_YEAR:
		// break;
		// }
		release(g, width, height, true);
	}

	/*
	 * if editing is being performed traversal cannot be performed<br/> when
	 * fire button is pressed the state changes from traversal to editing and
	 * vice versa
	 */

	public void setSelectorBGColor(int selectedBgColor, int alphaLevel) {
		this.selectedBgColor = selectedBgColor;
	}

	public void setSelectorFGColor(int selectedForeColor) {
		this.selectedForeColor = selectedForeColor;
	}

	private void setVal() {
		day = calendar.get(Calendar.DAY_OF_MONTH);
		month = calendar.get(Calendar.MONTH);
		year = calendar.get(Calendar.YEAR);
		StringBuffer buf = new StringBuffer().append(day).append(':').append(month).append(':').append(year);
		date = buf.toString();
		// selectorWidth=
	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		// TODO Auto-generated method stub
		return false;
	}

	protected void updateModel() {
		highlitFont = Font.getFont(theme.widgetFont.getFace(), Font.STYLE_BOLD, theme.widgetFont.getSize());
		width = highlitFont.stringWidth("88:88:8888") + 4;
		height = highlitFont.getHeight() + 4;
		calendar = Calendar.getInstance();
		calendar.setTime(new java.util.Date(System.currentTimeMillis()));
		setVal();
		selectorX = 2;
	}

}
